using System;
using System.Windows.Forms;
using DataModel;
using DataModel.Reports;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace RitaReports.TravelAgency
{
    public partial class TravelAgencyFilter : XtraUserControl, IReportFilter
    {
        private bool inited;

        public bool Inited
        {
            get { return inited; }
        }

        public TravelAgencyFilter()
        {
            InitializeComponent();
        }

        #region IReportFilter Members

        public Control FilterControl
        {
            get { return this; }
        }

        public IReportFilterConditionds FilterConditionds
        {
            get
            {
                TravelAgencyFilterConditionds conditionds = new TravelAgencyFilterConditionds();

                if (cbAgencies.Properties.Items.Count > 0 &&
                    (int)cbAgencies.Properties.Items[cbAgencies.SelectedIndex].Value != 0)
                {
                    conditionds.AgencyId = (int)cbAgencies.Properties.Items[cbAgencies.SelectedIndex].Value;
                }

                if (deDateFrom.EditValue != null || deDateTo.EditValue != null)
                {
                    DateTime? from = null;
                    DateTime? to = null;

                    if (deDateFrom.EditValue != null)
                    {
                        from = (DateTime)deDateFrom.EditValue;
                    }

                    if (deDateTo.EditValue != null)
                    {
                        to = (DateTime)deDateTo.EditValue;
                    }

                    conditionds.Date = new Range<DateTime?>(from, to);
                }

                return conditionds;
            }
        }

        #endregion


        public void Init(TravelAgencyReportInitData initData)
        {
            cbAgencies.Properties.Items.Clear();
            cbAgencies.Properties.Items.Add(new ImageComboBoxItem("All", 0, 0));
            foreach (Organization org in initData.Agencies)
            {
                cbAgencies.Properties.Items.Add(new ImageComboBoxItem(org.Name, org.Id, 1));
            }
            cbAgencies.SelectedIndex = 0;
            inited = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cbAgencies.SelectedIndex = 0;
            deDateFrom.EditValue = null;
            deDateTo.EditValue = null;
        }
    }
}